package com.jinkeen.xiaxie.customView

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.util.AttributeSet
import android.util.TypedValue
import com.jinkeen.xiaxie.R

/**
 * @description: TODO
 * @author whz
 * @date 2021/12/18 11:09
 * @version 1.0
 */

class MyKeyboardView : KeyboardView {

    private var paddingTop: Int? = 0
    private var paint: Paint = Paint()
    private val iconRect:Rect=Rect()
    var grayBackgroundArrays = intArrayOf(
        Constants.KEYCODE_CLEAR,
        Constants.KEYCODE_HIDE,
        Constants.KEYCODE_ALL,
        Constants.KEYCODE_QUARTER,
        Constants.KEYCODE_HALF,
        Constants.KEYCODE_THIRD,
        Constants.KEYCODE_ADD_HUNDRED,
        Constants.KEYCODE_DELETE,
        Constants.KEYCODE_MINUS_HUNDRED,
        Constants.KEYCODE_SHIFT,
        Constants.KEYCODE_ONE_TWO_THREE,
        Constants.KEYCODE_DOWN,
        Constants.KEYCODE_SIX_ZERO_ZERO,
        Constants.KEYCODE_SIX_ZERO_ONE,
        Constants.KEYCODE_ZERO_ZERO_ZERO,
        Constants.KEYCODE_ZERO_ZERO_TWO,
        Constants.KEYCODE_THREE_ZERO_ZERO
    )

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet!!, 0)
    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        paddingTop = getPaddingTop()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val keys = getKeyboard().keys
        keys.forEach {
            if (needGrayBackground(it.codes[0])) {
                //全键盘
//                if (mK)
            }
            //删除图标
            if (it.codes[0] == Keyboard.KEYCODE_DELETE) {
                drawIconKey(it,canvas!!, R.drawable.btn_shape_normal)
            }
        }

    }

    private fun dip2px(dip: Int): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dip.toFloat(),
            resources.displayMetrics
        )

    }

    private fun needGrayBackground(code: Int): Boolean {
        grayBackgroundArrays.forEach {
            if (code == it) {
                return true
            }
        }
        return false
    }

    //绘制带有icon的按键
    private fun drawIconKey(key: Keyboard.Key, canvas: Canvas?, drawable: Int) {
        val mDeleteDrawable = resources.getDrawable(drawable)
        //计算删除图标绘制的坐标
        var drawWidth = 0
        var drawHeight = 0
        val intrinsicWidth = mDeleteDrawable.intrinsicWidth
        val intrinsicHeight = mDeleteDrawable.intrinsicHeight
        val mDeleteWidth = dip2px(24)
        val mDeleteHeight = dip2px(24)
        if (mDeleteWidth > 0 && mDeleteHeight > 0) {
            drawWidth = mDeleteWidth.toInt()
            drawHeight = mDeleteHeight.toInt()
        } else if (mDeleteWidth > 0) {
            drawWidth = mDeleteWidth.toInt()
            drawHeight = drawWidth * intrinsicHeight / intrinsicWidth
        } else if (mDeleteHeight > 0) {
            drawHeight = mDeleteHeight.toInt()
            drawWidth = drawHeight * intrinsicWidth / intrinsicHeight
        } else {
            drawWidth = intrinsicWidth
            drawHeight = intrinsicHeight
        }
        //限制图标大小，防止图标超出按键
        if (drawWidth > key.width) {
            drawWidth = key.width
            drawHeight = drawWidth
        }

        if (drawHeight > key.height) {
            drawHeight = key.height
            drawWidth = drawHeight * intrinsicWidth / intrinsicHeight
        }
        //获取删除图标绘制的坐标
        val left=key.x+(key.width-drawWidth)/2+paddingLeft
        val top=key.y+(key.height-drawHeight)/2+paddingTop!!
        iconRect.set(left,top,left+drawWidth,top+drawHeight)
        // 绘制删除的图标
        mDeleteDrawable.setBounds(
            iconRect.left, iconRect.top,
            iconRect.right, iconRect.bottom
        )
        mDeleteDrawable.draw(canvas!!)
    }
}